package com.xie.web;

import com.xie.utils.LayUiDataUtil;
import com.xie.pojo.User;
import com.xie.service.BaiduWapService;
import com.xie.service.UserService;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("admin")
@PreAuthorize("hasRole('ADMIN')")
public class Admin {

    @Autowired
    UserService userService;

    @Autowired
    BaiduWapService baiduWapService;

    @RequestMapping("index")
    public ModelAndView toIndex(ModelAndView modelAndView){
        modelAndView.addObject("user_all",userService.countALl());
        modelAndView.addObject("url_all",baiduWapService.countAllBaiduWap());
        modelAndView.addObject("url_all_false",baiduWapService.findByFalse());
        modelAndView.addObject("url_all_tj",baiduWapService.countAllBaiduWapTj());
        modelAndView.setViewName("admin-index");
        return modelAndView;
    }

    @RequestMapping("user")
    public ModelAndView toUser(){
        return new ModelAndView("admin-user");
    }

    @RequestMapping("wapList")
    public ModelAndView toWapList(){
        return new ModelAndView("admin-wap");
    }

    @RequestMapping("wapTj")
    public ModelAndView toWapTj(){
        return new ModelAndView("admin-wapTj");
    }

    @RequestMapping("userData")
    public Map<String,Object> userList(@RequestParam(name = "page",defaultValue = "1") Integer page,
                                       @RequestParam(name = "limit",defaultValue = "10") Integer limit){
        return new LayUiDataUtil(userService.findAll(page,limit),userService.countALl()).getData();
    }

    @RequestMapping("userInfo")
    public Map<String,Integer> userInfo(Integer id,String name){
        Map<String,Integer> map=new HashMap<>();
        map.put("userHave",userService.findZyById(id));
        map.put("userSub",userService.findTjZyByUser(name));
        return map;
    }
    @PostMapping("updateUser")
    public void updateUser(Integer id,Integer money){
        User user=new User();
        user.setMoney(money);
        user.setId(id);
        userService.updateMoney(user);
    }

    @RequestMapping("wapData")
    public Map<String,Object> wapData(@RequestParam(name = "page",defaultValue = "1") Integer page,
                                       @RequestParam(name = "limit",defaultValue = "10") Integer limit) {
        return new LayUiDataUtil(baiduWapService.findAll(page,limit),baiduWapService.countAllBaiduWap()).getData();
    }

    @RequestMapping("del")
    public void delWap(Integer id){
        baiduWapService.deleteZyWap(id);
    }

    @RequestMapping("wapTjData")
    public Map<String,Object> wapTjData(@RequestParam(name = "page",defaultValue = "1") Integer page,
                                        @RequestParam(name = "limit",defaultValue = "10") Integer limit){
        return new LayUiDataUtil(baiduWapService.findTjAll(page,limit),baiduWapService.countAllBaiduWapTj()).getData();
    }

}
